
/*
 * @Descripttion: 日期扩展
 * @version: 
 * @Author: ada.wu
 * @Date: 2025-07-02 02:24:59
 * @LastEditors: ada.wu
 * @LastEditTime: 2025-07-03 13:46:09
 */
layui.define(["element", "laytpl", "jquery", "miniPage"], function (exports) {
    var element = layui.element,
        $ = layui.$,
        laytpl = layui.laytpl,
        layer = layui.layer;
    miniPage = layui.miniPage;


    var miniDate = {
        // 格式化日期为 YYYY-MM-DD
        formatDate: function (date) {
            const y = date.getFullYear();
            const m = (date.getMonth() + 1).toString().padStart(2, '0');
            const d = date.getDate().toString().padStart(2, '0');
            return `${y}-${m}-${d}`;
        },
        // 获取当前月份的开始和结束日期
        getCurrentMonthRange: function () {
            const now = new Date();
            const year = now.getFullYear();
            const month = now.getMonth();
            const firstDay = new Date(year, month, 1);
            const lastDay = new Date(year, month + 1, 0);
            return {
                start: miniDate.formatDate(firstDay),
                end: miniDate.formatDate(lastDay)
            };
        },
        /**
         * 获取指定年月的开始和结束日期
         * @param {Number} year 年份
         * @param {Number} month 月份 (1~12)
         * @returns {Object} 包含 start 和 end 的日期字符串
         */
        getMonthRange: function (year, month) {
            if (!year || !month || month < 1 || month > 12) {
                throw new Error("请输入有效的年份和月份（1~12）");
            }
            const firstDay = new Date(year, month - 1, 1); // JS 中月份从 0 开始
            const lastDay = new Date(year, month, 0);
            return {
                start: this.formatDate(firstDay),
                end: this.formatDate(lastDay)
            };
        },


        isToday: function (dateStr) {
            const today = miniDate.formatDate(new Date()); // 获取今天的日期字符串
            return dateStr === today;
        },
        // 格式化日期为 YYYY-MM-DD
        getDateWithWeekArray: function (startDateStr, endDateStr) {
            const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
            const startDate = new Date(startDateStr);
            const endDate = new Date(endDateStr);
            const dateList = [];

            let currentDate = new Date(startDate);

            while (currentDate <= endDate) {
                const dateStr = miniDate.formatDate(currentDate);
                const dayOfWeek = weekDays[currentDate.getDay()];
                // 新增：提取 MM-DD 格式
                const mm = (currentDate.getMonth() + 1).toString().padStart(2, '0');
                const dd = currentDate.getDate().toString().padStart(2, '0');
                const monthDay = `${mm}-${dd}`;
                const day = `${dd}`;

                dateList.push({
                    day: day,
                    date: monthDay,
                    fullDate: dateStr,
                    weekDay: dayOfWeek,
                    isToday: miniDate.isToday(dateStr)
                });
                currentDate.setDate(currentDate.getDate() + 1);
            }

            return dateList;
        }


    };


    exports("miniDate", miniDate);
});
